/*
 * CookController.java
 * Copyright (c) 2014, 2019, Kiri. 
 * All rights reserved.
 */
package com.onesalt.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.onesalt.domain.Kitchen;
import com.onesalt.domain.User;
import com.onesalt.service.KitchenService;
import com.onesalt.service.UserService;
import com.onesalt.utils.JSONUtil;

/**
 * 厨子网络服务
 *
 * @author kiri
 * @date 2015年3月5日
 * @version 1.0
 */
@Controller
@RequestMapping("/cooks")
public class CookController extends AbstractController {

	private final UserService userService;
	
	private final KitchenService kitchenService;

	@Autowired
	public CookController(UserService userService, KitchenService kitchenService) {
		this.userService = userService;
		this.kitchenService = kitchenService;
	}
	
	public UserService getUserService() {
		return this.userService;
	}

	public KitchenService getKitchenService() {
		return kitchenService;
	}

	@RequestMapping(value = "", method = RequestMethod.GET)
	public String findAll(@RequestParam(defaultValue = "0") int pageIndex, @RequestParam(defaultValue = "10") int pageSize,
			@RequestParam(required = false) String sn, @RequestParam(required = false) String name,
			@RequestParam(required = false) String city, @RequestParam(required = false) Long kitchenId, @RequestParam(required = false) String kitchenName,
			@RequestParam(required = false) String tel, @RequestParam(required = false) String mobile,
			@RequestParam(required = false) Integer startAge, @RequestParam(required = false) Integer endAge,
			@RequestParam(required = false) String applyStartTime, @RequestParam(required = false) String applyEndTime,
			@RequestParam(required = false) String registerStartTime, @RequestParam(required = false) String registerEndTime,
			@RequestParam(required = false) String notes, Model model) {

		getLogger().debug("pageIndex = " + pageIndex + ", pageSize = " + pageSize);
		Date parsedApplyStartTime = null;
		Date parsedApplyEndTime = null;
		Date parsedRegisterStartTime = null;
		Date parsedRegisterEndTime = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			parsedApplyStartTime = StringUtils.hasText(applyStartTime) ? sdf.parse(applyStartTime) : null;
			parsedApplyEndTime = StringUtils.hasText(applyEndTime) ? sdf.parse(applyEndTime) : null;
			parsedRegisterStartTime = StringUtils.hasText(registerStartTime) ? sdf.parse(registerStartTime) : null;
			parsedRegisterEndTime = StringUtils.hasText(registerEndTime) ? sdf.parse(registerEndTime) : null;
		}
		catch (ParseException ex) {
			getLogger().debug(ex.getMessage(), ex);
		}

		Page<User> cooks = getUserService().findAllCooks(sn, name, city, tel, mobile, kitchenId, kitchenName, startAge, endAge,
				parsedApplyStartTime, parsedApplyEndTime, parsedRegisterStartTime, parsedRegisterEndTime, notes, pageIndex, pageSize);
		model.addAttribute("page", cooks);
		List<String> cities = getUserService().findAllCities();
		model.addAttribute("cities", cities);
		
		getLogger().debug("cooks = " + JSONUtil.toJsonString(cooks));
		return "cook-list";
	}

	@RequestMapping(value = "/{sn}", method = RequestMethod.GET)
	public String findBySn(@PathVariable String sn, @RequestParam(defaultValue = "false") boolean edit, Model model) {
		User user = getUserService().getUserRepository().findBySn(sn);
		getLogger().info("cook = " + JSONUtil.toJsonString(user));
		if (user != null) {
			model.addAttribute("user", user);
			model.addAttribute("type", edit ? "edit" : "info");
			return "cook";
		} else {
			return "error";
		}
	}

	@RequestMapping(value = "/{user}", method = RequestMethod.POST)
	public String update(@ModelAttribute User user, Model model) {
		getLogger().info("cook = " + JSONUtil.toJsonString(user));
		User result = getUserService().getUserRepository().save(user);
		if (result != null) {
			model.addAttribute("user", result);
			model.addAttribute("type", "info");
			return "cook";
		}
		else {
			return "error";
		}
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(Model model) {
		User user = new User();
		user.setKitchen(new Kitchen());
		model.addAttribute("user", user);
		model.addAttribute("type", "add");
		return "cook";
	}

	@RequestMapping(value = "", method = RequestMethod.POST)
	public String add(@ModelAttribute User user, BindingResult br, Model model) {
		user.setCreateTime(new Date());
		user.setCookApplyTime(new Date());
		
		Kitchen kitchen = user.getKitchen();
		kitchen.setCity(user.getCity());
		user = getUserService().createCook(user, kitchen);
		
		if (user != null) {
			model.addAttribute("user", user);
			model.addAttribute("type", "info");
			return "cook";
		}
		
		return "error";
	}
}
